package com.zbf.web;

import com.alibaba.fastjson.JSON;
import com.zbf.common.ResponseResult;
import com.zbf.common.ResultStatus;
import com.zbf.common.SysInfo;
import com.zbf.ex.LoginFailException;
import com.zbf.service.UserService;
import com.zbf.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/6/1  15:21
 * 描述:
 */
@RestController
@RequestMapping("login")
public class LoginController {

    @Autowired
    private UserService userService;


    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @RequestMapping("toLogin")
    public ResponseResult toLogin(@RequestBody Map<String,String> map, HttpServletRequest request){
        //校验用户名或者密码是空的情况
        if(map.get("userName")==null||map.get("password")==null||"".equals(map.get("userName"))||"".equals(map.get("password"))){
            throw new LoginFailException("用户名或者密码不存在！");
        }

        //1、根据用户名查询用户信息
        Map<String, Object> user = userService.getUserByUserName(map.get("userName"));
        if(user!=null){
            //2、校验密码
            //加密前端传递过来的密码
            String password = MD5.encryptPassword(map.get("password"), user.get("salt").toString());
            //比对和数据库的密码是不是正确
            if(password.equals(user.get("passWord").toString())){
               //登录 成功了
                //3、存储登录信息
                //存储信息到session
               request.getSession().setAttribute(SysInfo.userInfo, JSON.toJSONString(user));
               //用户的权限信息放入redis中一份
                Map<String, Object> userUrlMenu = userService.getUserMenusByUserId(user.get("id").toString());
                redisTemplate.opsForHash().putAll(SysInfo.userQuanXian+user.get("id"),(Map<String, Object>)userUrlMenu.get("menusUrl1"));

                //返回前端用户数据
                user.remove("passWord");
                user.remove("salt");
                user.put("urlList",userUrlMenu.get("menusUrl2"));
                ResponseResult responseResult = ResponseResult.getResponseResult().setCode(ResultStatus.AUTH_LOGIN_SUCCESS.getCode())
                        .setMsg(ResultStatus.AUTH_LOGIN_SUCCESS.getMsg())
                        .setResult(user);
                //4、返回登录信息
                return responseResult;
            }else {
                throw new LoginFailException("用户名或者密码不存在！");
            }

        }else{
            throw new LoginFailException("用户名或者密码不存在！");
        }
    }


    @RequestMapping("toLoginOut")
    //@AuthPromiss("toLoginOut")
    public ResponseResult toLoginOut(HttpServletRequest request){

        //清除session中的数据
        request.getSession().removeAttribute(SysInfo.userInfo);


        ResponseResult responseResult = ResponseResult.getResponseResult().setCode(ResultStatus.AUTH_LOGINOUT_SUCCESS.getCode())
                .setMsg(ResultStatus.AUTH_LOGINOUT_SUCCESS.getMsg());

        return responseResult;
    }

}
